//
// Created by Criinal Nicole on 2020/6/19.
//

#include "Menu.h"

void Menu::init() {
    int oper = 10;
    Controller::initialize();
    while (oper) {
        cout << "命令菜单:\n"
                "1.格式化\n"
                "2.登录\n"
                "3.创建用户\n";
        Controller::curUserId = -1;
        cin >> oper;
        if (oper == 1) {
            Controller::format();
        } else if (oper == 2) {
            cout << "请输入用户名：\n";
            char _name[USER::USER_NAME_SIZE];
            cin.getline(_name, USER::USER_NAME_SIZE);
            cin.getline(_name, USER::USER_NAME_SIZE);
            auto i = USER::findUser(_name);
            if (!i.isValid()) {
                cout << "不存在用户！\n";
                continue;
            }
            cout << "请输入密码：\n";
            char _pswd[USER::PASSWORD_SIZE];
            cin.getline(_pswd, USER::PASSWORD_SIZE);
            if (i.checkPassword(_pswd)) {
                cout << "登录成功！\n";
                Controller::curUserId = i._userId;
//                func();
            } else {
                continue;
            }
        } else if (oper == 3) {
            cout << "请输入用户名：\n";
            char _name[USER::USER_NAME_SIZE];
            cin.getline(_name, USER::USER_NAME_SIZE);
            cin.getline(_name, USER::USER_NAME_SIZE);
            auto i = USER::findUser(_name);
            if (i.isValid()) {
                cout << "用户已存在！\n";
                continue;
            }
            USER::User newUser;
            cout << "请输入密码：\n";
            char _pswd[USER::PASSWORD_SIZE];
            cin.getline(_pswd, USER::PASSWORD_SIZE);
            int priv;
            cout << "请输入权限等级(1-5)\n";
            cin >> priv;
            newUser._privilege = priv;
            newUser.setName(_name);
            newUser.setPassword(_pswd);
            if (USER::addUser(newUser) != -1) {
                cout << "创建成功\n";
                Controller::store();
            } else {
                cout << "用户数量已达上限\n";
            }
        }
    }
}
